package org.bspv.pharma.dao.impl;

import java.util.Date;
import java.util.List;

import org.bspv.pharma.dao.CredentialDAO;
import org.bspv.pharma.model.User;
import org.bspv.pharma.model.security.Credential;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "credentialDAO")
public class CredentialDAOImpl extends AbstractHibernateDAO<Credential, Long>
		implements CredentialDAO {

	@Autowired
	public CredentialDAOImpl(SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public List<Credential> findValidCredentials(User user, Date date) {
		DetachedCriteria crit = DetachedCriteria.forClass(getManagedType());
		crit.add(Restrictions.eq("user", user));
		crit.add(Restrictions.le("beginning", date));
		crit.add(Restrictions.ge("end", date));
		return getHibernateTemplate().findByCriteria(crit);
	}

}
